<?php

namespace App\Console\Commands;

use App\model\customer\Customer;
use App\model\customer\CustomerRemind;
use App\Notifications\CustomerRemindNotification;
use App\User;
use Carbon\Carbon;
use Illuminate\Console\Command;

class UserFollowRemind extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'userFollow:remind';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '客户跟进提醒';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        Customer::whereStatus(Customer::FOLLOW_TO_USER)->chunkById(100,function($customers){
            foreach ($customers as $customer){
                if(Carbon::parse($customer->record_time)->addDays(7)->lt(Carbon::now())){
                    $customer->is_chongfen=1;
                    $customer->save();
                }
            }
        });

        Customer::whereStatus(Customer::FOLLOW_TO_USER)->where('follow_status','!=',Customer::wuxiao)->chunkById(100,function ($customers){

            foreach ($customers as $customer){

                /**
                 * @var Customer $customer
                 */
               $customerRemind= CustomerRemind::where('user_id',$customer->ower_user_id)
                   ->where('customer_id',$customer->customer_id)
                   ->orderBy('created_at','desc')
                   ->first();
                //查询跟进时间记录
                if (Carbon::now()->addMinutes(-30)->gt(Carbon::parse($customer->record_time))){
                    //发送提醒记录
                    if (!$customerRemind){
                        User::find($customer->ower_user_id)->notify(new CustomerRemindNotification($customer));
                        $customerRemind=new CustomerRemind();
                        $customerRemind->user_id=$customer->ower_user_id;
                        $customerRemind->customer_id=$customer->customer_id;
                        $customerRemind->save();
                        $this->info('30fenzhong');
                    }
                }
                if ((Carbon::now()->diffInDays(Carbon::parse($customer->record_time)))/3>=1&&(Carbon::now()->diffInDays(Carbon::parse($customer->record_time)))/3<=3){
                    if (!$customerRemind||Carbon::parse($customerRemind->created_at)->addDays(3)->lt(Carbon::now())){
                        User::find($customer->ower_user_id)->notify(new CustomerRemindNotification($customer));
                        $customerRemind=new CustomerRemind();
                        $customerRemind->user_id=$customer->ower_user_id;
                        $customerRemind->customer_id=$customer->customer_id;
                        $customerRemind->save();
                        $this->info('3天');
                    }
                }

            }
        });
    }
}
